package com.zs.ssh.dao.impl;

import com.zs.ssh.dao.ISessionUtilDao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * 持久化封装
 *
 * @author: 赵圣
 * @date: 2018年9月27日下午4:15:19
 * @Version 1.0
 */
@SuppressWarnings("all")
@Transactional
@Component
public class SessionUtilDaoImpl implements ISessionUtilDao {
    private static Logger logger = LoggerFactory.getLogger(SessionUtilDaoImpl.class);

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public boolean delete(Object obj) {
        try {
            if (obj == null) {
                logger.error("saveOrUpdate obj is null ");
                return false;
            }
            Session session = sessionFactory.getCurrentSession();
            session.delete(obj);
            return true;
        } catch (Exception e) {
            logger.error("删除记录失败==> " + e.getMessage(), e);

        }
        return false;
    }

    @Override
    public boolean saveOrUpdate(Object obj) {
        try {
            if (obj == null) {
                logger.error("saveOrUpdate obj is null ");
                return false;
            }
            Session session = sessionFactory.getCurrentSession();
            session.saveOrUpdate(obj);
            return true;
        } catch (Exception e) {
            logger.error("保存记录失败==> " + e.getMessage(), e);
        }
        return false;
    }

}
